package com.ycfmedical.bed.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.listener.IvsModel;
import com.ycfmedical.bed.local.DataManager;
import com.ycfmedical.bed.model.VsModel;
import com.ycfmedical.bed.present.MainPresenter;
import com.ycfmedical.bed.ui.fragment.BedControlFragment;
import com.ycfmedical.bed.ui.fragment.CallNurseFragment;
import com.ycfmedical.bed.ui.fragment.EntertainFragments;
import com.ycfmedical.bed.ui.fragment.HISFragment;
import com.ycfmedical.bed.ui.fragment.HealthNewsFragment;
import com.ycfmedical.bed.ui.fragment.PhysicianChargeFragment;
import com.ycfmedical.bed.ui.fragment.UserCenterFragment;
import com.ycfmedical.bed.ui.fragment.VsFragment;
import com.ycfmedical.bed.widgets.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.droidlover.xdroidmvp.base.XFragmentAdapter;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import cn.droidlover.xdroidmvp.router.Router;


/**
 * Created by yf on 2017-03-30.
 */

public class
MainActivity extends XActivity<MainPresenter> implements VsFragment.callBack {

    @BindView(R.id.tabMain)
    TabLayout tabMain;
    @BindView(R.id.fragmentList_main)
    NoScrollViewPager viewPagerMain;

    @BindView(R.id.bed_id)
    TextView bed_id;
    @BindView(R.id.bed_doctor)
    TextView bed_doctor;
    @BindView(R.id.bed_name)
    TextView bed_name;
    @BindView(R.id.bed_nurse)
    TextView bed_nurse;
    @BindView(R.id.bed_heart)
    TextView bed_heart;
    @BindView(R.id.bed_weight)
    TextView bed_weight;
    @BindView(R.id.bed_breathe)
    TextView bed_breathe;
    @BindView(R.id.bed_wifi)
    TextView bed_wifi;

    XFragmentAdapter mAdapter;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private String[] titles = {"床体控制", "个人中心", "HIS系统", "医嘱", "生命体征", "呼叫帮助", "休闲娱乐", "健康头条"  };
    private List<TextView> textViews = new ArrayList<>();

    @Override
    public void initData(Bundle savedInstanceState) {
//         viewPagerMain.setNoScroll(true);
        if(DataManager.getInstance().getUser()==null){
            getvDelegate().toastShort("用户没有登录呀");
            finish();
        }
        addText();
        initViewPager();
        initTab();
        setSelectedTab();
        getP().showCard();
    }

    private void addText() {
        textViews.add(bed_id);
        textViews.add(bed_doctor);
        textViews.add(bed_name);
        textViews.add(bed_nurse);
        textViews.add(bed_heart);
        textViews.add(bed_breathe);
        textViews.add(bed_weight);
        textViews.add(bed_wifi);

        int size = textViews.size();
        TextView textView = textViews.get(0);
        IvsModel vsModel = VsModel.getVsModel();
    }

    public void setCardText(ArrayList<String> data) {
        for(int i = 0;i<data.size();i++){
            textViews.get(i).setText(data.get(i));
        }
    }

    /**
     * 根据intent传入的ID设置默认选中的tab
     */
    private void setSelectedTab() {

        int selectedId = getIntent().getIntExtra(Constants.SHARE_HOMETAB_KEY, 0);
        if (selectedId < tabMain.getTabCount() && null != tabMain.getTabAt(selectedId)) {
            tabMain.getTabAt(selectedId).select();
        }
    }

    private void initViewPager() {
        fragmentList.clear();
        fragmentList.add(BedControlFragment.newInstance());
        fragmentList.add(UserCenterFragment.newInstance());
        fragmentList.add(new HISFragment());
        fragmentList.add(new PhysicianChargeFragment());
        fragmentList.add(new VsFragment());
        fragmentList.add(new CallNurseFragment());
        fragmentList.add(new EntertainFragments());
        fragmentList.add(new HealthNewsFragment());
        if (mAdapter == null) {
            mAdapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
        }
        viewPagerMain.setAdapter(mAdapter);
        viewPagerMain.setOffscreenPageLimit(fragmentList.size());

        tabMain.setupWithViewPager(viewPagerMain);
    }

    /**
     * 设置tab图片
     */
    private void initTab() {
        tabMain.setTabMode(TabLayout.GRAVITY_FILL);
        setTabIcon(tabMain.getTabAt(0), R.drawable.icon_bedcontrol_selector, Constants.TAG_BED_CONTROL);
        setTabIcon(tabMain.getTabAt(1), R.drawable.icon_usercenter_selector, Constants.TAG_USER_CENTER);
        setTabIcon(tabMain.getTabAt(2), R.drawable.icon_his_selector, Constants.TAG_HIS);
        setTabIcon(tabMain.getTabAt(3), R.drawable.icon_physician_charge_selector, Constants.TAG_PHYSICIAN_CHARGE);
        setTabIcon(tabMain.getTabAt(4), R.drawable.icon_vistalsign_selector, Constants.TAG_VITAL);
        setTabIcon(tabMain.getTabAt(5), R.drawable.icon_callnurse_selector, Constants.TAG_CALL_NURSE);
        setTabIcon(tabMain.getTabAt(6), R.drawable.icon_entertain_selector, Constants.TAG_ENTERTAIN);
        setTabIcon(tabMain.getTabAt(7), R.drawable.icon_healthnews_selector, Constants.TAG_HEALTH_NEWS);
        //初始化生命体征数据传输方法
        VsModel.getVsModel().receiveData("");
    }

    /**
     * 自定义tabView
     */
    private void setTabIcon(TabLayout.Tab tab, int tabIcon, String tag) {
        if (null != tab) {
            String title = tab.getText().toString();
            tab.setTag(tag);
            View view = getLayoutInflater().inflate(R.layout.tab_custom_view, null);
            TextView titleTV = (TextView) view.findViewById(R.id.tabTitle);
            ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
            ivIcon.setImageResource(tabIcon);
            titleTV.setText(title);
            tab.setCustomView(view);

        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public MainPresenter newP() {
        return new MainPresenter();
    }

    public static void launch(Activity activity, int pagerValue) {
        Router.newIntent(activity).putInt(Constants.SHARE_HOMETAB_KEY, pagerValue).to(MainActivity.class).launch();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void deliver(String[] data) {
        for(int i = 0;i < data.length;i++){
                textViews.get(i+4).setText(data[i]);
        }
    }
}
